const Router = require('koa-router');
const path = require('path');
const fs = require('fs');
const router = new Router();

const templateDir = 'views/';

router.get('/', ctx => {
  console.log(ctx.body);
  ctx.render('index.html');
});

//一级页面
router.get('/:page', ctx => {
  const page = ctx.params.page;
  if (/\.html$/.test(page)) {
    ctx.render(page);
  } else if (fs.existsSync(
    path.join(process.cwd(), templateDir, page + '.html'))) {
    ctx.render(page + '.html');
  } else {
    ctx.render(page + '/index.html');
  }
});
//二级页面
router.get('/:project/:page', ctx => {
  const project = ctx.params.project;
  const page = ctx.params.page;
  if (/\.html$/.test(page)) {
    ctx.render(path.join(project, page));
  } else {
    ctx.render(path.join(project, page + '.html'));
  }
});
module.exports = router;